package com.cagtwitter.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cagtwitter.services.BaseParams;
import com.cagtwitter.services.DatabaseServices;
import com.cagtwitter.services.WebDataServices;

/**
 * Servlet implementation class Celebrities
 */
@WebServlet("/Celebrities")
public class Celebrities extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Celebrities() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	// Parameters 	:	query
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String query = request.getParameter("query");
		//response.setContentType("text/html; charset=UTF-8");
		PrintWriter out = response.getWriter();
		String result = "";
		
		if(query.equals("getCelebrities")){
			DatabaseServices dbs = new DatabaseServices();
			result = dbs.getCelebritiesAsJSon(null);
		}
		else if(query.equals("getUserData")){
			WebDataServices wds = new WebDataServices();
			result = wds.getUserData(request.getParameter(BaseParams.SCREEN_NAME));
		}
		else if(query.equals("getCelebritiesThatMatches")){
			DatabaseServices dbs = new DatabaseServices();
			result = dbs.getCelebritiesAsJSon(request.getParameter("match_string"));
		}
		else if(query.equals("getCelebrityTweets")){
			DatabaseServices dbs = new DatabaseServices();
			result = dbs.getCelebTweetsAsJSon(request.getParameter("screen_name"),Integer.parseInt(request.getParameter("page")),Integer.parseInt(request.getParameter("count")));
		}
		else if(query.equals("getAboutCelebrityTweets")){
			DatabaseServices dbs = new DatabaseServices();
			result = dbs.getAboutCelebTweetsAsJSon(request.getParameter("name"),Integer.parseInt(request.getParameter("page")),Integer.parseInt(request.getParameter("count")));
		}
		
		
		
	    out.println(result);
	    out.close();
	}

}